package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;

    /**
     * 新增菜品，同时保存对应的口味数据
     *
     * @param dishDto
     */
    @Override
    @Transactional  //开启事务管理
    public void saveWithFlavor(DishDto dishDto) {
        //保存菜品的基本信息到菜品表dish
        this.save(dishDto);
        //获取菜品id
        Long dishId = dishDto.getId();

        //获取要保存的菜品口味数据
        List<DishFlavor> flavors = dishDto.getFlavors();
        //stream流的map()接收一个函数作为参数，该函数会被应用到每个元素上，并将其映射成一个新的元素
        //也可以使用集合遍历的方式添加dishId
        flavors = flavors.stream().map(item -> {
            item.setDishId(dishId);
            return item;
        }).collect(Collectors.toList());

        //保存菜品口味数据到菜品口味表dish_flavor
        dishFlavorService.saveBatch(flavors);

    }

    /**
     * 根据id查询菜品信息和对应的口味信息
     *
     * @param id
     * @return
     */
    @Override
    public DishDto getByIdWithFlavor(Long id) {
        //查询菜品基本信息，从dish表查询
        Dish dish = this.getById(id);

        //创建DishDto对象，用于封装口味表的口味信息
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish, dishDto);

        //查询当前菜品对应的口味信息，从dish_flavor表查询
        LambdaQueryWrapper<DishFlavor> lqw = new LambdaQueryWrapper<>();
        lqw.eq(DishFlavor::getDishId, dish.getId());
        List<DishFlavor> dishFlavors = dishFlavorService.list(lqw);

        //将查询到的口味信息封装到DishDto中
        dishDto.setFlavors(dishFlavors);

        return dishDto;


    }

    /**
     * 修改菜品信息
     *
     * @param dishDto
     */
    @Override
    @Transactional
    public void updateWithFlavor(DishDto dishDto) {
        //更新dish表基本信息
        this.updateById(dishDto);

        //清理当前菜品对应口味数据---dish_flavor表的delete操作
        LambdaQueryWrapper<DishFlavor> dishFlavorLQW = new LambdaQueryWrapper<>();
        dishFlavorLQW.eq(DishFlavor::getDishId, dishDto.getId());
        //执行删除口味操作
        dishFlavorService.remove(dishFlavorLQW);

        //添加当前提交过来的口味数据---dish_flavor表的insert操作
        //拿到提交的口味数据
        List<DishFlavor> flavors = dishDto.getFlavors();
        //将口味数据与菜品id建立关联
        flavors = flavors.stream().map((item) -> {
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());

        //保存新的口味数据
        dishFlavorService.saveBatch(flavors);

    }

    /**
     * 删除菜品信息
     *
     * @param ids
     */
    @Override
    public void removeWithFlavor(List<Long> ids) {
        //根据是否起售，判断是否可以删除
        LambdaQueryWrapper<Dish> dishLWQ = new LambdaQueryWrapper<>();
        dishLWQ.in(Dish::getId, ids);
        dishLWQ.eq(Dish::getStatus, 1);

        int count = this.count(dishLWQ);
        if (count > 0) {
            //起售状态，提示不可以删除
            throw new CustomException("该菜品仍在售中，不可删除");
        }
        //停售状态，执行删除
        this.removeByIds(ids);
        //删除菜品对应的口味信息
        LambdaQueryWrapper<DishFlavor> dishFlavorLWQ = new LambdaQueryWrapper<>();
        dishFlavorLWQ.in(DishFlavor::getDishId, ids);
        dishFlavorService.remove(dishFlavorLWQ);
    }
}
